home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / TURBO PASCAL 1.5 for WIN / PAINT.PAK / LINEBAR.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-06-08  |  3.1 KB  |  122 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Pascal for Windows: Paint Demo         }
  4. {   LineBar unit                                 }
  5. {   Copyright (c) 1992 by Borland International  }
  6. {                                                }
  7. {************************************************}
  8.  
  9. unit LineBar;
  10.  
  11. { This unit defines a line thickness selection window for the paint program.
  12.   The line bar is responsible for displaying the available and current line
  13.   widths and provides the interface to select the current line width.
  14. }
  15.  
  16. interface
  17.  
  18. uses PaintDef, WinTypes, WinProcs, WObjects;
  19.  
  20. const
  21.   LineCount = 8;    { Number of line widths available }
  22.   LineBarWidth = LineCount * 4 + 6 + (1 + 2 + 3 + 4 + 5 + 7 + 9 + 12);
  23.               { Total width of window }
  24.  
  25. type
  26.  
  27.   PLineBar = ^TLineBar;
  28.   TLineBar = object(TWindow)
  29.     State: PState;
  30.     
  31.     { Creation }
  32.     constructor Init(AParent: PWindowsObject; AState: PState);
  33.  
  34.     { Display }
  35.     procedure Paint(PaintDC: HDC; var PaintInfo: TPaintStruct); virtual;
  36.  
  37.     { Window manager interface }
  38.     procedure WMLButtonDown(var Msg: TMessage);
  39.       virtual wm_First + wm_LButtonDown;
  40.   end;
  41.  
  42.  
  43. implementation
  44.  
  45. const
  46.   LineWidth: array[0..LineCount - 1] of Integer = (
  47.     1, 2, 3, 4, 5, 7, 9, 12);         { The available line widths }
  48.  
  49. { Create a line bar.
  50. }
  51. constructor TLineBar.Init(AParent: PWindowsObject; AState: PState);
  52. begin
  53.   TWindow.Init(AParent, nil);
  54.   Attr.Style := ws_Border or ws_Child or ws_Visible;
  55.   State := AState;
  56. end;
  57.  
  58. { Draw the line bar. A sample line of each availble is drawn vertically
  59.   (samples arrayed horizontally).
  60.   Each sample line is drawn by filling in the rectangle it occupies rather
  61.   than as a true line for ease of computation of position.
  62. }
  63. procedure TLineBar.Paint(PaintDC: HDC; var PaintInfo: TPaintStruct);
  64. var
  65.   I, X, W: Integer;  { Sample number; X position of sample; Sample width }
  66.   R: TRect;         { Rect that sample will occupy }
  67.  
  68. { Draw triangular notch marks to indicate the currently selected width.
  69. }
  70. procedure Notch(Y, DY: Integer);
  71. var
  72.   L: Integer;
  73. begin
  74.   for L := 3 downto 0 do
  75.   begin
  76.     MoveTo(PaintDC, X + W div 2 - L, Y);
  77.     LineTo(PaintDC, X + W div 2 + L + 1, Y);
  78.     Inc(Y, DY);
  79.   end;
  80. end;
  81.  
  82. begin
  83.   X := 4;
  84.   for I := 0 to LineCount - 1 do
  85.   begin
  86.     { Draw the line sample }
  87.     W := LineWidth[I];
  88.     SetRect(R, X, 5, X + W, 25);
  89.     FillRect(PaintDC, R, GetStockObject(black_Brush));
  90.  
  91.     { Mark the currently selected width }
  92.     if W = State^.PenSize then
  93.     begin
  94.       Notch(0, 1);
  95.       Notch(29, -1);
  96.     end;
  97.     Inc(X, W + 4);
  98.   end;
  99. end;
  100.  
  101. { Set the currently selected line widht to be that whose sample line was
  102.   pressed and update the display.
  103. }
  104. procedure TLineBar.WMLButtonDown(var Msg: TMessage);
  105. var
  106.   I, X, W: Integer;
  107. begin
  108.   X := 2;
  109.   for I := 0 to LineCount - 1 do
  110.   begin
  111.     W := LineWidth[I];
  112.     if (Msg.LParamLo >= X) and (Msg.LParamLo < X + W + 4) then
  113.     begin
  114.       State^.PenSize := W;
  115.       InvalidateRect(HWindow, nil, True);
  116.       Exit;
  117.     end;
  118.     Inc(X, W + 4);
  119.   end;
  120. end;
  121.  
  122. end.